<?php
/**
 * Created by PhpStorm.
 * User: johnxu
 * Date: 2019-03-12
 * Time: 16:57
 */

namespace app\admin\controller;

use app\admin\model\Users;
use think\Controller;

class Login extends Controller
{
    /**
     * 登录
     *
     * @return mixed
     */
    public function index()
    {
        if ( $this->request->isPost() )
        {
            $username = $this->request->post( 'username' );
            $password = $this->request->post( 'password' );
            $res      = Users::login( $username, $password );
            if ( $res['type'] !== 'system' )
            {
                $this->error( '账号或密码错误' );
            }

            $res ? $this->success( '登录成功' ) : $this->error( '账号或密码错误' );
        }
        else
        {
            // 检测是否登录，已经登录直接去后台首页
            if ( session( '?auth_user' ) )
            {
                $this->redirect( '/admin' );
            }

            return $this->fetch();
        }
    }

    /**
     * 退出成功，到登录页面
     */
    public function logout()
    {
        session( 'auth_user', null );

        $this->success( '退出成功', url( 'login/index' ) );
    }
}
